package com.sixth.service.impl;

import com.sixth.bean.MarketSystem;
import com.sixth.bean.MarketSystemExample;

import com.sixth.mapper.MarketSystemMapper;
import com.sixth.service.AdminConfigService;
import com.sixth.util.ConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/7 22:05
 **/

@Service
public class AdminConfigServiceImpl implements AdminConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Transactional
    @Override
    public void update(Map<String, Object> map, List<String> list) {
        List<MarketSystem> marketSystems = select(list);
        //check哪些值需要修改
        List<MarketSystem> changes = ConfigUtils.getChanges(map, marketSystems);
        //根据id修改list中的value值和updateTime
        int code = 0;
        for (MarketSystem change : changes) {
            code = marketSystemMapper.updateByPrimaryKeySelective(change);
            if(code == 0){
                throw new RuntimeException("修改失败");
            }
        }
    }

    @Override
    public List<MarketSystem> select(List<String> list) {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        criteria.andKeyNameIn(list);
        return marketSystemMapper.selectByExample(marketSystemExample);
    }


}
